package com.bsoft.gol.hcs.his.service.impl;

import com.bsoft.gol.hcs.clinical.bean.qo.DoctorListQo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import ctd.controller.exception.ControllerException;
import ctd.spring.boot.annotation.SsdevService;
import ctd.util.annotation.RpcService;
import hcn.base.Organization;
import hcn.util.BaseResponse;
import com.bsoft.gol.hcs.his.bean.request.InHospitalpatientListQueryRequest;
import com.bsoft.gol.hcs.his.bean.response.InHospitalPatientResponse;
import com.bsoft.gol.hcs.his.service.InHospitalPatientService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import service.rpc.impl.OrganizationRpcServiceImpl;
import service.rpc.impl.PcnIdentityServiceImpl;
import com.bsoft.gol.hcs.utils.CommonUtils;
import com.bsoft.gol.hcs.utils.Constants;
import org.atag.util.http.KdHttpClientUtil;
import org.atag.dao.model.SimplePageInfo;
import com.bsoft.gol.hcs.visitrecord.dao.VrInpatientVisitRecordDAO;

import java.util.List;
import java.util.Map;

/**
 * 住院患者his接口
 *
 * @author lxy
 * @date 2020/7/18
 */
@SsdevService("inHospitalPatientInfo")
public class InHospitalPatientServiceImpl implements InHospitalPatientService {


    private final static Logger log = LoggerFactory.getLogger(InHospitalPatientServiceImpl.class);

    @Autowired
    private VrInpatientVisitRecordDAO vrInpatientVisitRecordDAO;

    @Autowired
    private PcnIdentityServiceImpl pcnIdentityServiceImpl;

    @Autowired
    private OrganizationRpcServiceImpl organizationRpcService;

    /**
     * 获取住院病人列表-废弃
     *
     * @param request request
     * @return list
     */
    /*@Override
    public BaseResponse<List<InHospitalPatientResponse>> listPatient(InHospitalpatientListQueryRequest request) {
        log.info("listPatient：{}", request.toString());
        JSONObject jsonParams = (JSONObject) JSONObject.toJSON(request);
        //入参转换
        jsonParams.put("organizationId", jsonParams.remove("hospitalCode"));
        jsonParams.put("doctorId", jsonParams.remove("docJobNumber"));
        jsonParams.put("departmentId", jsonParams.remove("departmentCode"));
        JSONObject json = HttpClient.doMainPost(Constants.HIS_CLIENT_URL_01 + Constants.LIST_PATIENT, jsonParams);
        log.info("listPatient 第三方返回数据：{}", json);
        BaseResponse<List<InHospitalPatientResponse>> baseResponse = new BaseResponse<>();
        if (null != json && json.getInteger("code") == Constants.SUCCESS_CODE) {
            if (json.containsKey("data") && json.get("data") != null && !json.getJSONArray("data").isEmpty()) {
                List<InHospitalPatientResponse> inHospitalPatientResponseList = new ArrayList<>();
                List<InHospitalPatientTemporary> inHospitalPatientTemporaries =
                        JSONArray.parseArray(json.getJSONArray("data").toJSONString(),
                                InHospitalPatientTemporary.class);
                for (InHospitalPatientTemporary inHospitalPatientTemporary : inHospitalPatientTemporaries) {
                    InHospitalPatientResponse inHospitalPatientResponse = new InHospitalPatientResponse();
                    String patientSex = inHospitalPatientTemporary.getPatientSex();
                    if (StringUtils.isNotEmpty(patientSex)) {
                        switch (patientSex) {
                            case "男":
                                inHospitalPatientTemporary.setPatientSex("1");
                                break;
                            case "女":
                                inHospitalPatientTemporary.setPatientSex("2");
                                break;
                            default:
                                inHospitalPatientTemporary.setPatientSex("9");
                        }
                    }
                    BeanUtils.copyProperties(inHospitalPatientTemporary, inHospitalPatientResponse);
                    inHospitalPatientResponse.setPatientCode(inHospitalPatientTemporary.getPatientId());
                    inHospitalPatientResponseList.add(inHospitalPatientResponse);
                }
                baseResponse.setData(inHospitalPatientResponseList);
            } else {
                baseResponse.setCode(Constants.SUCCESS_CODE);
                baseResponse.setMessage(json.getString("message"));
            }
        } else {
            baseResponse.setCode(Constants.ERR_CODE);
            if (json != null) {
                baseResponse.setMessage(json.getString("message"));
            }
        }
        log.info("=listPatient-出参=>>>" + baseResponse.toString());
        return baseResponse;
    }*/


    /**
     * 获取住院病人列表-最新
     *
     * @param request request
     * @return list
     */
    @Override
    @RpcService
    public InHospitalPatientResponse queryInHospitalPatientList(InHospitalpatientListQueryRequest request) throws ControllerException {
//        log.info("InHospitalPatientServiceImpl.queryInHospitalPatientList 入参：{}", JSONObject.toJSON(request));
//        BaseResponse<InHospitalPatientResponse> result = new BaseResponse<InHospitalPatientResponse>();
//        JSONObject jsonParams = JSONObject.parseObject(JSONObject.toJSONString(request));
////        String testUrl = "http://interfacedemo.zk.atag.bsoft.com.cn/inHospitalPatient/listpatient";
//        //JSONObject jsonObject = HttpClient.doPostJSONRequest(Constants.HIS_CLIENT_URL_01 + Constants.OUTPATIENT_LIST_PATIENT, jsonParams);
////        JSONObject jsonObject = HttpClient.doPostJSONRequest(testUrl, jsonParams);
//        String jsonContext = JSONDateUtil.readFile("inHospitalPatient");
//        JSONObject jsonObject =JSONObject.parseObject(jsonContext);
//        log.info("InHospitalPatientServiceImpl.queryInHospitalPatientList 第三方返回数据:{}", JSONObject.toJSONString(jsonObject));
//        try {
//            if (null != jsonObject && jsonObject.getInteger("code") == ResultCode.SUCCESS) {
//                InHospitalPatientResponse response = new InHospitalPatientResponse();
//                if (jsonObject.containsKey("data") && jsonObject.get("data") != null) {
//                    response = JSONObject.parseObject(jsonObject.get("data").toString(), InHospitalPatientResponse.class);
//                }
//                result.setData(response);
//                log.info("InHospitalPatientServiceImpl.queryInHospitalPatientList 出参：{}", JSONObject.toJSONString(result));
//            } else {
//                result.setCode(jsonObject.getInteger("code"));
//                result.setMessage(jsonObject.getString("message"));
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            result.setMessage(Constants.JSON_ANALYSIS_ERR + e.getMessage());
//            result.setCode(ResultCode.ERROR);
//        }
//        return result;

        JSONObject jsonParams = JSONObject.parseObject(JSONObject.toJSONString(request));
        String url = Constants.COMMON_PROXY_URL + Constants.IN_HOSPITAL_LIST_PATIENT;
        JSONObject jsonObject = KdHttpClientUtil.doPostJSONRequest(url, jsonParams);
        BaseResponse<InHospitalPatientResponse> result = JSON.parseObject(jsonObject.toJSONString(), BaseResponse.class);
        return result.getData();
    }

    /**
     * 获取住院患者列表
     *
     * @param request 列表查询入参
     * @return 住院患者列表
     */
    @Override
    @RpcService
    public SimplePageInfo<InHospitalPatientResponse.InHospitalPatient> queryLocalInHospitalPatientList(InHospitalpatientListQueryRequest request) {
        SimplePageInfo<InHospitalPatientResponse.InHospitalPatient> hospitalPatientList = vrInpatientVisitRecordDAO.queryLocalInHospitalPatientList(request);
        if (CommonUtils.isNotEmpty(hospitalPatientList)){
            String mpiId= null;
            for (InHospitalPatientResponse.InHospitalPatient patient : hospitalPatientList.getList()) {
                if (CommonUtils.isNotEmpty(patient.getOrganizationId())) {
                    Organization orgDetailByOrgCode = organizationRpcService.getOrgDetailByOrgCode(patient.getOrganizationId());
                    mpiId= pcnIdentityServiceImpl.findMpiIdByPatientCode(orgDetailByOrgCode.getOrgId(),patient.getPatientId());
                }
                if (CommonUtils.isEmpty(mpiId)) {
                    patient.setPersonId(patient.getPatientId());
                } else {
                    patient.setPersonId(mpiId);
                }
            }
        }
        return hospitalPatientList;
    }

    @Override
    @RpcService
    public List<Map<String, String>> inPatientDoctorList(DoctorListQo request) {
        List<Map<String, String>> mapList = vrInpatientVisitRecordDAO.inPatientDoctorList(request.getOrganizationId());
        return mapList;
    }

}
